package com.emperor.service.impl;

import com.emperor.dto.NodeDto;
import com.emperor.entity.Business;
import com.emperor.mapper.NodeMapper;
import com.emperor.service.NodeService;
import com.emperor.vo.RegionDataVo;
import com.emperor.vo.SkuClassPageVo;
import com.emperor.vo.VmListVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NodeServiceImpl implements NodeService {
    @Autowired
    private NodeMapper nodeMapper;


    @Override
    public SkuClassPageVo findAll(Integer pageIndex, Integer pageSize, String regionId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<RegionDataVo> list = nodeMapper.findAll(regionId);
        Page<RegionDataVo> p = (Page<RegionDataVo>) list;
        // 计算总页数
        long l = (p.getTotal() / pageSize) + 1;
        // 创建SkuClassPageVo对象
        SkuClassPageVo build = SkuClassPageVo.builder().
                pageIndex(pageIndex).
                pageSize(pageSize)
                .totalPage((int) l).totalCount((int) p.getTotal())
                .currentPageRecords(new List[]{p.getResult()}).build();
        // 返回SkuClassPageVo对象
        return build;

    }

    @Override
    public List<Business> findBusiness() {
        List<Business> businesses = nodeMapper.findBusiness();
        return businesses;
    }

    @Override
    public VmListVo findNodeById(Integer nodeId) {
        return nodeMapper.findNodeById(nodeId);
    }

    @Override
    public void updateNode(Integer nodeId, NodeDto dto) {
        nodeMapper.updateNode(nodeId, dto);
    }

    @Override
    public void deleteNode(Integer id) {
        nodeMapper.deleteNode(id);
    }

    @Override
    public Integer count() {
       return nodeMapper.count();
    }
}
